package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.QuoteException;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.POIUtils;
import com.itheima.utils.QiniuUtils;
import com.itheima.vo.OrderSettingVo;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.io.filefilter.OrFileFilter;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    //文件上传，并解析文件内容并保存到数据库
    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) {
        try {
            //解析文件内容
            List<String[]> list = POIUtils.readExcel(multipartFile);
            if (list != null && list.size() > 0) {
                //创建一个集合,吧预约的数据存进去
                List<OrderSetting> orderSettingList = new ArrayList<>();
                //解析文件的内容,strings[0] 预约日期,strings[1] 可预约人数
                for (String[] strings : list) {
                    //判断预约的设置日期是否已过期
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
                    if (simpleDateFormat.parse(strings[0]).getTime()> new Date().getTime()) {
                        OrderSetting orderSetting = new OrderSetting(simpleDateFormat.parse(strings[0]), Integer.parseInt(strings[1]));
                        orderSettingList.add(orderSetting);
                    } else {
                        throw new QuoteException();
                    }

                }//吧文件中的数据添加到数据库中
                orderSettingService.add(orderSettingList);
            }

        } catch (Exception e) {
            return new Result(false, e.getMessage());
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);

    }

    //查询所有预约管理数据
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month) {

        try {
            List<OrderSettingVo> list = orderSettingService.findByMonth(month);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, list);
        } catch (QuoteException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
    }

    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByOrderDate(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);//失败
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);//成功

    }
}
